package com.shaodongbo.demo_4;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IonDelListener {

    private ListView stuListView;
    private  ArrayList<Student> studentArrayList = new ArrayList<>();
    private  ArrayList<Student> searchResultList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;

    private EditText edtKeyword;
    private Button btnSeach;
    private boolean isResultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStuList();
        
        initAdapter();
        
        initListView();

        initAddBtn();

        initSearchView();
    }

    /**
     * 初始化搜索模块
     */
    private void initSearchView() {
        edtKeyword = findViewById(R.id.edt_keyword);
        btnSeach = findViewById(R.id.btn_search);

        btnSeach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = edtKeyword.getText().toString().trim();
                search(studentArrayList, keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });

        edtKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = edtKeyword.getText().toString().trim();
                search(studentArrayList, keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });
    }

    /**
     * 使用学生列表和关键字 得到搜索结果列表
     * @param studentArrayList
     * @param keyword
     */
    private void search(ArrayList<Student> studentArrayList, String keyword) {
        searchResultList.clear();
        for (int i = 0; i < studentArrayList.size(); i++){
            Student s = studentArrayList.get(i);

//            如果学生姓名包含了关键字 这个学生就加入到结果列表
            if (s.getName().contains(keyword)){
                searchResultList.add(s);
            }
//            如果学生姓名包含了关键字 这个学生就加入到结果列表
            else if (String.valueOf(s.getAge()).equals(keyword)){
                searchResultList.add(s);
            }
        }
    }

    /**
     * 初始化添加学生按钮
     */
    private void initAddBtn() {
        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    /**
     * 初始化ListView
     */
    private void initListView() {
        stuListView = findViewById(R.id.name_listView);

        stuListView.setAdapter(adapter);

        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Student s = studentArrayList.get(position);

                if (isResultList){
                    s = searchResultList.get(position);
                }

                Toast.makeText(MainActivity.this, "姓名" + s.getName() + "年龄" + s.getAge(), Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);
                intent.putExtra("NAME", s.getName());
                intent.putExtra("AGE", s.getAge());
                intent.putExtra("IMGID", s.getImgid());
                intent.putExtra("INDEX", position);
                startActivityForResult(intent, 1002);
            }
        });
    }

    /**
     *初始化适配器
     */
    private void initAdapter() {
        adapter = new StuAdapter(MainActivity.this, studentArrayList, MainActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (data == null){
            return;
        }

        if (resultCode == 2001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgID = data.getIntExtra("IMG", R.drawable.dog1);

            Student s = new Student(imgID, name, age);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        }

        else if (resultCode == 2002){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgID = data.getIntExtra("IMG", R.drawable.dog1);
            int index = data.getIntExtra("INDEX", 0);

            Student s = studentArrayList.get(index);
            s.setImgid(imgID);
            s.setName(name);
            s.setAge(age);

            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 初始化学生列表的数据
     */
    private void initStuList() {
        Student s1 = new Student(R.drawable.dog1, "张三", 18);
        Student s2 = new Student(R.drawable.dog2, "赵四", 19);
        Student s3 = new Student(R.drawable.dog3, "王五", 20);
        Student s4 = new Student(R.drawable.dog4, "赵柳", 21);
        Student s5 = new Student(R.drawable.dog5, "董卓", 22);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    @Override
    public void del(int i) {
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
